package com.jf.cloud.order.controller.multishop;

import com.google.common.collect.Lists;
import com.jf.cloud.common.order.dto.OrderSelfStationDTO;
import com.jf.cloud.common.order.vo.OrderItemVO;
import com.jf.cloud.common.order.vo.OrderSelfStationVO;
import com.jf.cloud.common.order.vo.OrderVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.order.constant.RefundStatusEnum;
import com.jf.cloud.order.service.OrderItemService;
import com.jf.cloud.order.service.OrderRefundService;
import com.jf.cloud.order.service.OrderSelfStationService;
import com.jf.cloud.order.service.OrderService;
import com.jf.cloud.order.vo.OrderRefundVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController("multishopOrderSelfStationController")
@RequestMapping("/m/order_self_station")
@Tag(name = "商家端自提订单接口")
public class OrderSelfStationController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderSelfStationService orderSelfStationService;
    @Autowired
    private OrderRefundService orderRefundService;
    @Autowired
    private OrderItemService orderItemService;

    @GetMapping("/get_order_item_and_station_info")
    @Operation(summary = "获取提货订单详情")
    public ServerResponseEntity<OrderSelfStationVO> getOrderItemAndStationInfo(@RequestParam("orderId") Long orderId) {
        OrderVO orderVO = orderService.getOrderByOrderId(orderId);
        List<OrderItemVO> orderItemVOS = orderItemService.listOrderItemAndLangByOrderId(orderId);
        orderVO.setOrderItems(orderItemVOS);
        OrderSelfStationVO orderSelfStation = orderSelfStationService.getStationByOrderId(orderId);
        orderSelfStation.setOrderStatus(orderVO.getStatus());
        if (Objects.nonNull(orderVO.getRefundStatus()) && !Objects.equals(orderVO.getRefundStatus(), RefundStatusEnum.DISAGREE.value())) {
            List<OrderRefundVO> refundVOList = orderRefundService.getProcessingOrderRefundByOrderId(orderVO.getOrderId());
            for (OrderRefundVO orderRefund : refundVOList) {
                //整单退款
                if (orderRefund.getRefundType() == 1) {
                    for (OrderItemVO orderItem : orderVO.getOrderItems()) {
                        orderItem.setReturnMoneySts(orderRefund.getReturnMoneySts());
                    }
                    break;
                }
                // 单项退款，每个单号都不一样
                for (OrderItemVO orderItem : orderVO.getOrderItems()) {
                    if (Objects.equals(orderItem.getOrderItemId(), orderRefund.getOrderItemId())) {
                        orderItem.setReturnMoneySts(orderRefund.getReturnMoneySts());
                    }
                }
            }
        }
        orderSelfStation.setOrderItems(orderVO.getOrderItems());
        return ServerResponseEntity.success(orderSelfStation);
    }

    @PutMapping("/change_status_and_order_station")
    @Operation(summary = "自提订单发货")
    public ServerResponseEntity<Void> changeStatusAndOrderStation(@RequestBody OrderSelfStationDTO orderSelfStationDTO) {
        List<Long> orderIdList = Lists.newArrayList();
        orderIdList.add(orderSelfStationDTO.getOrderId());
        orderService.orderStationByOrderId(orderIdList, null, orderSelfStationDTO.getStationId());
        return ServerResponseEntity.success();
    }
}
